home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / Javacup / PR8ADPL7.TAR / productivity_tools / PR8ADPL7 / ImageButton.java < prev    next >
Encoding:
Java Source  |  1996-05-23  |  3.2 KB  |  145 lines

  1. // ImageButton.java
  2. // A button with an image on it. Can be set to indent on a mouse click, or
  3. // when the mouse pointer moves over it.
  4. import java.awt.*;
  5.  
  6. public class ImageButton extends Canvas
  7. {
  8.     Image im1, im2;        // non-indented and indented images
  9.     boolean indent;        // indent on mouse-over?
  10.     int border;        // pixel size of 3d border
  11.     Color col1, col2;    // colours for border
  12.     boolean down;        // are we indented right now?
  13.     int w,h;        // max image size
  14.  
  15.     // Create a button using the same image for both indented and
  16.     // non-indented modes.
  17.     ImageButton(Image i)
  18.     {
  19.     this(i, i, false, 3);
  20.     }
  21.  
  22.     // Create an imagebutton with the given iamge and border
  23.     ImageButton(Image i, int b)
  24.     {
  25.     this(i, i, false, b);
  26.     }
  27.  
  28.     // Create a button with non-indented and indented images
  29.     ImageButton(Image i1, Image i2)
  30.     {
  31.     this(i1, i2, false, 3);
  32.     }
  33.     
  34.     // Create a button with non-indented and indented images, and the
  35.     // given indent mode and border size.
  36.     ImageButton(Image i1, Image i2, boolean id, int b)
  37.     {
  38.     im1 = i1; im2 = i2;
  39.     indent = id;
  40.     border = b;
  41.     // Wait for the image to properly load
  42.     MediaTracker wt = new MediaTracker(this);
  43.     wt.addImage(im1, 666);
  44.     wt.addImage(im2, 666);
  45.     try wt.waitForAll();
  46.     catch(InterruptedException e);
  47.     w = Math.max(im1.getWidth(this), im2.getWidth(this));
  48.     h = Math.max(im1.getHeight(this), im2.getHeight(this));
  49.  
  50.     // alloc colours
  51.     col1 = new Color(220, 220, 220);
  52.     col2 = new Color(50, 50, 50);
  53.     }
  54.  
  55.     // paint
  56.     // Re-render this component
  57.     public void paint(Graphics g)
  58.     {
  59.     if (g == null) return;
  60.     // Paint border
  61.     int w = size().width, h = size().height;
  62.     g.setColor(down?col2:col1);
  63.     for(int i=0; i<border; i++) {
  64.         g.drawLine(i,i,w-i,i);
  65.         g.drawLine(i,i,i,h-i);
  66.         }
  67.     g.setColor(down?col1:col2);
  68.     for(int i=0; i<border; i++) {
  69.         g.drawLine(w-i,h-i, w-i,i);
  70.         g.drawLine(w-i,h-i, i,h-i);
  71.         }
  72.  
  73.     // Paint image
  74.     if (isEnabled())
  75.         g.drawImage(down?im2:im1, border, border,
  76.                 w-border*2, h-border*2, this);
  77.     else {
  78.         g.setColor(Color.lightGray);
  79.         g.fillRect(border, border, w-border*2, h-border*2);
  80.         }
  81.     }
  82.  
  83.     // mouseDown
  84.     // If we are not indenting whenever the pointer moves over this
  85.     // component, indent now.
  86.     public boolean mouseDown(Event evt, int x, int y)
  87.     {
  88.     if (!isEnabled()) return false;
  89.     if (!indent) {
  90.         down = true;
  91.         paint(getGraphics());
  92.         }
  93.     return true;
  94.     }
  95.  
  96.     // mouseUp
  97.     // Send a message to the parent component
  98.     public boolean mouseUp(Event evt, int x, int y)
  99.     {
  100.     if (!isEnabled()) return false;
  101.     if (!indent) {
  102.         down = false;
  103.         paint(getGraphics());
  104.         }
  105.     if (x >= 0 && y >= 0 && x < size().width && y < size().height)
  106.         getParent().postEvent(new Event(this,Event.ACTION_EVENT,null));
  107.     return true;
  108.     }
  109.  
  110.     // mouseEnter
  111.     // If in auto-indent mode, indent this button
  112.     public boolean mouseEnter(Event evt, int x, int y)
  113.     {
  114.     if (!isEnabled()) return false;
  115.     if (indent) {
  116.         down = true;
  117.         paint(getGraphics());
  118.         }
  119.     return true;
  120.     }
  121.  
  122.     // mouseExit
  123.     // If in auto-indent mode, un-indent this button
  124.     public boolean mouseExit(Event evt, int x, int y)
  125.     {
  126.     if (!isEnabled()) return false;
  127.     if (indent) {
  128.         down = false;
  129.         paint(getGraphics());
  130.         }
  131.     return true;
  132.     }
  133.  
  134.     public Dimension minimumSize()
  135.     {
  136.     return new Dimension(w+border*2, h+border*2);
  137.     }
  138.  
  139.     public Dimension preferredSize()
  140.     {
  141.     return minimumSize();
  142.     }
  143. }
  144.  
  145.